package com.jvxb.manage.livable.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Condition;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jvxb.common.constants.DbConstants;
import com.jvxb.common.constants.WorkOrderConstants;
import com.jvxb.common.utils.BeanUtil;
import com.jvxb.common.utils.CommonUtil;
import com.jvxb.manage.livable.entity.DbDeployInfo;
import com.jvxb.manage.livable.entity.WorkOrder;
import com.jvxb.manage.livable.service.*;
import com.jvxb.manage.livable.service.dbDeploy.BaseDeployService;
import com.jvxb.manage.livable.util.WorkOrderUtil;
import com.jvxb.manage.livable.vo.ClusterApplyVo;
import com.jvxb.manage.livable.vo.Employee;
import com.jvxb.manage.livable.vo.WorkOrderOperateVo;
import com.jvxb.modules.configuration.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jvxb
 * @since 2022-01-07
 */
@Service
public class DbDeployServiceImpl implements DbDeployService {

    @Autowired
    WorkOrderService workOrderService;
    @Autowired
    DbDeployInfoService dbDeployInfoService;
    @Autowired
    AssetsAgentHostService agentHostService;
    @Autowired
    Map<String, BaseDeployService> deployServiceMap;

    @Override
    public void checkBeforeApply(ClusterApplyVo clusterApplyVo) {
        Assert.hasLength(clusterApplyVo.getDbType(), "类型检测异常：请指定部署的数据库类型！");
        Assert.isTrue(clusterApplyVo.getVersion().replace(".", "").matches("\\d+"), "版本号检查异常：请录入并指定数据库版本号！");
        DbDeployInfo dbDeployInfo = new DbDeployInfo();
        BeanUtil.copy(clusterApplyVo, dbDeployInfo);
        deployService(clusterApplyVo.getDbType()).checkAndGetResource(dbDeployInfo, false);
    }

    @Override
    @Transactional
    public Long apply(ClusterApplyVo clusterApplyVo) {
        //保存工单信息
        WorkOrder workOrder = WorkOrderUtil.createClusterApplyOrder(clusterApplyVo);
        workOrderService.save(workOrder);
        //保存申请信息
        clusterApplyVo.setWorkOrderId(workOrder.getId());
        clusterApplyVo.setCreateTime(workOrder.getCreateTime());
        deployService(clusterApplyVo.getDbType()).saveApply(clusterApplyVo);
        return workOrder.getId();
    }

    @Override
    public void executeOrder(WorkOrderOperateVo workOrderOperateVo) {
        //开始异步部署DB
        DbDeployInfo dbDeployInfo = dbDeployInfoService.getOne(Condition.<DbDeployInfo>create().eq(DbDeployInfo.WORK_ORDER_ID, workOrderOperateVo.getId()));
        deployService(dbDeployInfo.getDbType()).deploy(dbDeployInfo, workOrderOperateVo);
    }

    @Override
    public Map getDeployInfoByWorkOrderId(String dbType, Long workOrderId) {
        DbDeployInfo dbDeployInfo = dbDeployInfoService.getOne(Condition.<DbDeployInfo>create().eq(DbDeployInfo.WORK_ORDER_ID, workOrderId));
        return JSONObject.parseObject(JSON.toJSONString(dbDeployInfo), Map.class);
    }

    private BaseDeployService deployService(String dbType) {
        return deployServiceMap.get(dbType + DbConstants.DEPLOY_SERVICE);
    }

}
